package com.ccapp.wsclient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.StrictMode;
import android.util.Log;

public class WebServiceUtil {
	static final String SERVICE_NS = "http://ws.util/";
	static final String SERVICE_URL = "http://192.168.1.102:8080/callcenterapp/service/ccapp";

	public static List<CcappObject> getCallInfo(CcappObject lsr) {

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());

		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath()
				.build());

		List<CcappObject> ccobs = new ArrayList<CcappObject>();

		final String methoName = "getTask";
		final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);

		ht.debug = true;

		final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		SoapObject soapobject = new SoapObject(SERVICE_NS, methoName);

		CcappObject cob = new CcappObject();
		cob.setCount(100);
		cob.setStaff_id("13999846280");

		// soapobject.addProperty("CcappObject", cob);

		soapobject.addProperty("CcappObject", "13999846280");

		// Log.i("webservice", soapobject.toString());
		envelope.dotNet = false;
		envelope.bodyOut = soapobject;

		try {
			ht.call(null, envelope);

			if (envelope.getResponse() != null) {

				SoapObject sob = (SoapObject) envelope.bodyIn;

				Log.i("webservice0", ht.responseDump);
				Log.i("webservice1", sob.getPropertyCount() + "");
				Log.i("webservice2", sob.getProperty(0) + "");

				SoapObject detail1 = (SoapObject) sob.getProperty(0);
				Log.i("webservice3", detail1.getProperty("cust_name") + "");

				for (int i = 0; i < sob.getPropertyCount(); i++) {
					SoapObject detail = (SoapObject) sob.getProperty(i);

					CcappObject cc = new CcappObject();
					cc.setCall_tag(detail.getProperty("call_tag").toString());
					cc.setCount(Integer.parseInt(detail.getProperty("count")
							.toString()));
					cc.setCust_name(detail.getProperty("cust_name").toString());
					cc.setEnd_time(detail.getProperty("end_time").toString());
					cc.setEparchy_code(detail.getProperty("eparchy_code")
							.toString());
					cc.setRemark(detail.getProperty("remark").toString());
					cc.setSerial_number(detail.getProperty("serial_number")
							.toString());
					cc.setStart_time(detail.getProperty("start_time")
							.toString());
					cc.setTask_id(Integer.parseInt(detail
							.getProperty("task_id").toString()));

					ccobs.add(cc);
					Log.i("webservice", detail.getProperty("serial_number")
							.toString());
					Log.i("webservice", detail.getProperty("cust_name")
							.toString());
				}

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	public static CcappObject putCallInfo(List<CcappObject> lsr) {
		CcappObject cc = new CcappObject();

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());

		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath()
				.build());

		final String methoName = "setTask";
		final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);

		ht.debug = true;

		final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		SoapObject soapobject = new SoapObject(SERVICE_NS, methoName);

		for (int i = 0 ; i < lsr.size() ; i ++ ){
			soapobject.addProperty("CcappObject", lsr.get(i));
		}
		
		envelope.bodyOut = soapobject;

		try {
			ht.call(null, envelope);
			Log.i("webservice0", ht.requestDump);
			Log.i("webservice0", ht.responseDump);
			if (envelope.getResponse() != null) {


				SoapObject sob = (SoapObject) envelope.bodyIn;

				Log.i("webservice0", ht.responseDump);
				Log.i("webservice1", sob.getPropertyCount() + "");
				Log.i("webservice2", sob.getProperty(0) + "");

				SoapObject detail1 = (SoapObject) sob.getProperty(0);
				Log.i("webservice3", detail1.getProperty("cust_name") + "");

				if(sob.getPropertyCount() <= 0 ){
					return null;
				}

				SoapObject detail = (SoapObject) sob.getProperty(0);

				cc.setResultCode(Integer.valueOf(detail.getProperty("resultCode").toString()));
				cc.setResultInfo(detail.getProperty("resultInfo").toString());
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return cc;
	}

}
